home *** CD-ROM | disk | FTP | other *** search
- Path: web.cae.ca!usenet
- From: fraserh@cae.ca (Fraser Hutchinson)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q]Dereference within class - Possible?
- Date: 18 Mar 1996 19:38:15 GMT
- Organization: CAE Electronics Ltd.
- Message-ID: <4ike37$rck@web.cae.ca>
- References: <Do566t.F1H.0.queen@torfree.net>
- NNTP-Posting-Host: pch63.cae.ca
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <Do566t.F1H.0.queen@torfree.net>, bh332@freenet.toronto.on.ca
- says...
- >
- >
- >
- >Greetings,
- >
- >In normal C style, one can code the following example(C++ compiler);
- >
- >int main() {
- > int x = 1;
- > int &x1 = x;
- >
- > ...
- > ...
- >}
- >
- >Is it also possible to use the same syntax within a class without intializing
- >the referencer? Any information will be appreciated...
-
- No, it is not. Classes are similar to struct definitions; they must be
- instantiated before they can be initialized. This is what constructors do.
-
- For example, in C I doubt you would consider this:
-
- struct foo {
- int x = 100;
- char text[] = "my text string";
- };
-
- What you are doing is somewhat analogous to
-
- class Int {
- Int(Int &x) { val = x.GetVal(); }
- Int(int)
-
- int GetVal() { return x; }
- void SetVal(int x) { val = x; }
-
- protected:
- int val;
- }
-
- Int::Int(int x) : val(x) {}
-
-
- void main() {
-
- Int x(4)
- Int *y(x);
-
-
- }
-
- Hope I read this one right,
-
- regards,
-
- Fraser
-
-
-